import * as THREE from 'three'
import colorList from './color'
import Element from '../../../base/Element'
export default class SpaceV1 extends Element {
  constructor() {
    super()
    this.material
    this.mixer
    this.action
    this.lightGroup
    this.lightStepTime
    this.animateLight = false
    this.colorIndex = 0
    this.init()
  }
  init() {
    this.createLights()
    this.createLights1()
    this.createLights2()
    // this.initCircel()
  }
  initCircel() {
    this.board = this.resource.items['scene-v1']
    let textures = this.resource.items
    this.board.scene.traverse(function (obj) {
      if (obj.isMesh) {
        if (obj.name == 'Object_8') {
          obj.visible = false
        }
        obj.material.emissive = obj.material.color
        obj.material.emissiveMap = obj.material.map
      }
    })
    // this.board.scene.scale.set(150, 150, 150)
    this.board.scene.rotation.x = 0.7
    this.board.scene.rotation.y = 1
    this.board.scene.rotation.z = -0.6
    this.debugCircel()
    this.mixer = new THREE.AnimationMixer(this.board.scene)
    this.action = this.mixer.clipAction(this.board.animations[0])
    this.action.loop = THREE.LoopRepeat
    this.scene.add(this.board.scene)
  }
  createLights() {
    // 圆环光源
    this.lightGroup = new THREE.Group()
    let pointPositions = []
    let r = 350
    for (let i = 0; i < 4; i++) {
      const rad = ((2 * Math.PI) / 4) * i + Math.PI / 4
      const x = Math.cos(rad) * r
      const z = Math.sin(rad) * r
      pointPositions.push([x, 30, z])
    }
    pointPositions.forEach((item) => {
      const pointLight = new THREE.PointLight(0xffffff, 1)
      pointLight.position.set(...item)
      // const pointLightHelper = new THREE.PointLightHelper(pointLight)
      this.lightGroup.add(pointLight)
    })
    this.scene.add(this.lightGroup)
    // this.debugLights()
  }
  createLights1() {
    // 圆环光源
    this.lightGroup = new THREE.Group()
    let pointPositions = []
    let r = 350
    for (let i = 0; i < 12; i++) {
      const rad = ((2 * Math.PI) / 12) * i + Math.PI / 12
      const x = Math.cos(rad) * r
      const z = Math.sin(rad) * r
      pointPositions.push([x, 100, z])
    }
    pointPositions.forEach((item) => {
      const pointLight = new THREE.PointLight(0xffffff, 1)
      pointLight.position.set(...item)
      // const pointLightHelper = new THREE.PointLightHelper(pointLight)
      this.lightGroup.add(pointLight)
    })
    this.scene.add(this.lightGroup)
    // this.debugLights()
  }
  createLights2() {
    // 圆环光源
    this.lightGroup = new THREE.Group()
    let pointPositions = []
    let r = 350
    for (let i = 0; i < 12; i++) {
      const rad = ((2 * Math.PI) / 12) * i + Math.PI / 12
      const x = Math.cos(rad) * r
      const z = Math.sin(rad) * r
      pointPositions.push([x, -100, z])
    }
    pointPositions.forEach((item) => {
      const pointLight = new THREE.PointLight(0xffffff, 1)
      pointLight.position.set(...item)
      // const pointLightHelper = new THREE.PointLightHelper(pointLight)
      this.lightGroup.add(pointLight)
    })
    this.scene.add(this.lightGroup)
    // this.debugLights()
  }
  debugLights() {
    const ui = this.debug.gui
    const groupFolder = ui.addFolder('环形光源组')
    const controls = new (function () {
      this.color = 0x000000
    })()
    groupFolder.addColor(controls, 'color').onChange((e) => {
      this.lightGroup.children.forEach((item) => {
        item.color = new THREE.Color(e)
      })
    })
  }
  debugCircel() {
    const ui = this.debug.gui
    const groupFolder = ui.addFolder('空间站')
    groupFolder.add(this.board.scene.position, 'x').min(-300).max(300).step(10).name('positionX')
    groupFolder.add(this.board.scene.position, 'y').min(-300).max(300).step(10).name('positionY')
    groupFolder.add(this.board.scene.position, 'z').min(-300).max(300).step(10).name('positionZ')
    groupFolder.add(this.board.scene.scale, 'x').min(1).max(50).step(5).name('scaleX')
    groupFolder.add(this.board.scene.scale, 'y').min(1).max(50).step(5).name('scaleY')
    groupFolder.add(this.board.scene.scale, 'z').min(1).max(50).step(5).name('scaleZ')
    groupFolder.add(this.board.scene.rotation, 'x').min(-1).max(1).step(0.1).name('rotationX')
    groupFolder.add(this.board.scene.rotation, 'y').min(-1).max(1).step(0.1).name('rotationY')
    groupFolder.add(this.board.scene.rotation, 'z').min(-1).max(1).step(0.1).name('rotationZ')
  }
  animate() {
    this.animateLight = true
    this.action.play()
  }
  stop() {
    this.action.paused = true
  }
  update() {
    this.mixer && this.mixer.update(this.time.delta)
    if (this.lightGroup && this.animateLight) {
      this.lightGroup.rotation.y += 0.003
      this.lightGroup.children.forEach((item) => {
        item.color = new THREE.Color(colorList[this.colorIndex])
      })
      this.colorIndex++
      if (this.colorIndex >= colorList.length) {
        this.colorIndex = 0
      }
    }
  }
}
